package org.vectrics.project.tags;

import java.util.Iterator;

import org.vectrics.common.html.Cell;
import org.vectrics.common.html.NameValueTable;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.DateUtil;
import org.vectrics.project.task.Task;
import org.vectrics.project.task.TaskResource;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;

public class TaskViewTag extends VectricsTagSupport {
	private String taskId = null;
	
	public TaskViewTag() {
		super();
	}

	/**
	 * @return Returns the taskId.
	 */
	public String getTaskId() {
		return taskId;
	}

	/**
	 * @param taskId The taskId to set.
	 */
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public Task getTask() {
		Long id = new Long(this.taskId);
		Task task = CoreServiceLocator.getProjectService().findTask(id);
		return(task);
	}
	
	protected String makeStartTag() {
		StringBuffer html = new StringBuffer();
		Table mainTable = new Table();
		
		Task task = this.getTask();

		Row mainRow = mainTable.addRow();
		Cell infoCell = mainRow.addCell();
		NameValueTable infoTable = new NameValueTable(this.getContextPath() + "graphics/");
		infoCell.addElement(infoCell);
		infoTable.addNameValueRow(this.getPhrase("prompt.task.id"), task.getId().toString());
		infoTable.addNameValueRow(this.getPhrase("prompt.task.reference.code"), task.getReferenceCode());
		infoTable.addNameValueRow(this.getPhrase("prompt.task.date.due"), CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(task.getDueDate()));
		infoTable.addNameValueRow(this.getPhrase("prompt.task.status.current"), this.getPhrase(task.getCurrentStatus().getTaskStatusType().getPhraseCode()));
		Iterator iter = task.getTaskResources().iterator();
		while (iter.hasNext()) {
			TaskResource taskResource = (TaskResource)iter.next();
			infoTable.addNameValueRow(this.getPhrase("prompt.task.resource"), taskResource.getPerson().getName());
		}
		
		html.append(mainTable.toString());
		return(html.toString());
	}

}
